<template>
  <el-card class="card">
    <el-row type="flex" justify="space-between" style="margin-bottom: 10px">
      <el-col :span="2">
        <el-button type="primary" @click="add">添加</el-button>
      </el-col>
      <el-col :span="6">
        <div style="display: flex">
          <el-input placeholder="根据名称搜索" v-model="keyword"></el-input>
          <el-button style="margin-left: 10px" @click="search">搜索</el-button>
        </div>
      </el-col>
    </el-row>
    <el-table :data="peoples" border>
      <el-table-column align="center" prop="name" label="借阅人员" />
      <el-table-column align="center" prop="book" label="借阅书籍" />
      <el-table-column align="center" prop="sTime" label="借阅时间" />
      <el-table-column align="center" prop="eTime" label="应还时间" />
      <el-table-column align="center" prop="status" label="是否超期">
        <template slot-scope="{ row }">
          <el-tag :type="!getStatus(row) ? 'danger' : 'success'" effect="dark">
            {{ !getStatus(row) ? "已超期" : "未超期" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="status" label="操作">
        <template slot-scope="{ row }">
          <span class="actions"
            ><i class="el-icon-date" @click="update(row)"></i
          ></span>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="visible" title="内容">
      <el-form label-position="left" label-width="100px">
        <el-form-item label="借阅人员">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="借阅书籍">
          <el-input v-model="form.book"></el-input>
        </el-form-item>
        <el-form-item label="借阅时间">
          <el-input v-model="form.sTime"></el-input>
        </el-form-item>
        <el-form-item label="应还时间">
          <el-input v-model="form.eTime"></el-input>
        </el-form-item>
        <el-form-item label="是否超期">
          <el-input v-model="form.status"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="addHandler">确定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { DB } from "@/utils";
import { peoples } from "./data.js";
const People = new DB("peoples");
if (People.isNull()) {
  People.init(peoples);
}
export default {
  data() {
    return {
      peoples: People.getList(),
      visible: false,
      form: {},
      keyword: "",
    };
  },
  methods: {
    getStatus(row) {
      const s = row.eTime.split("-");
      const e = new Date().toLocaleDateString().split("/");
      if (s[0] > e[0]) {
        return true;
      } else if (s[0] == e[0]) {
        if (s[1] > e[1]) {
          return true;
        } else if (s[1] == e[1]) {
          if (s[2] >= e[2]) {
            return true;
          } else {
            return false;
          }
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
    search() {
      this.peoples = People.getList(this.keyword);
    },
    cancel() {
      this.visible = false;
    },
    add() {
      this.visible = true;
    },
    addHandler() {
      if (this.form.id) {
        People.update(this.form);
      } else {
        People.add(this.form);
      }
      this.peoples = People.getList();
      this.visible = false;
      this.$message({ message: "操作成功", type: "success" });
      this.form = {};
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.visible = true;
    },
    remove(row) {
      People.delete(row.id);
      this.peoples = People.getList();
      this.$message({ message: "操作成功", type: "success" });
    },
  },
};
</script>

<style>
.actions {
  font-size: 18px;
  margin: 0 5px;
  cursor: pointer;
}
</style>